package com.findme.unvelouse;

import java.io.FileInputStream;
import java.io.IOException;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import com.findme.unvelouse.facade.VeloStationFacade;

public class FindMeUnVelouseActivity extends Activity {

	private static final int MENU_ABOUT = Menu.FIRST;

	private static final int DIALOG_ABOUT = 0;

	public static String FAVORI_FILE = "FAVORI_FILE";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// create the data
		VeloStationFacade.populate(getResources().openRawResource(R.raw.velotoulouse));
		this.loadFavoris();

		Button buttonAround = (Button) findViewById(R.id.aroundButton);
		buttonAround.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(FindMeUnVelouseActivity.this, VelouseGoogleMapsActivity.class);
				i.putExtra(VelouseGoogleMapsActivity.KEY_ADDRESS, (String) null);
				FindMeUnVelouseActivity.this.startActivity(i);
			}
		});

		Button buttonFind = (Button) findViewById(R.id.imageFind);
		buttonFind.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(FindMeUnVelouseActivity.this, FindAddressActivity.class);
				FindMeUnVelouseActivity.this.startActivity(i);
			}
		});

		Button buttonFavori = (Button) findViewById(R.id.favoriButton);
		buttonFavori.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(FindMeUnVelouseActivity.this, FavoriActivity.class);
				FindMeUnVelouseActivity.this.startActivity(i);
			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, MENU_ABOUT, 0, R.string.About);
		MenuItem item = menu.getItem(0);
		item.setIcon(android.R.drawable.ic_menu_info_details);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case MENU_ABOUT:
			showDialog(DIALOG_ABOUT);
		}

		return super.onMenuItemSelected(featureId, item);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		Dialog dialog;
		switch (id) {
		case DIALOG_ABOUT:
			dialog = new Dialog(this);

			dialog.setContentView(R.layout.aboutdialog);
			dialog.setTitle(R.string.About);

			break;
		default:
			dialog = null;
		}
		return dialog;
	}

	protected void loadFavoris() {

		VeloStationFacade facade = new VeloStationFacade();

		try {

			FileInputStream fIn = openFileInput(FindMeUnVelouseActivity.FAVORI_FILE);

			int ch;
			StringBuffer strContent = new StringBuffer("");

			while ((ch = fIn.read()) != -1)
				strContent.append((char) ch);

			// Transform the chars to a String
			String readString = strContent.toString();

			facade.updateFavoris(readString);

			fIn.read();
			fIn.close();

		} catch (IOException e) {
			Log.e("FindMeUnVelouse", e.getLocalizedMessage());
		}

	}
}